package top.tomxwd.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import top.tomxwd.springcloud.pojo.Dept;

import java.util.List;

/**
 * RestFul风格
 *
 * @author xieweidu
 * @createDate 2020-06-05 22:08
 */
@RestController
public class DeptConsumerController {

    @Autowired
    private RestTemplate restTemplate;

//    private static final String REST_URL_PREFIX = "http://localhost:8001";

    /**
     * 通过Ribbon负载均衡的时候，不应该写localhost:8080，而是提供服务的应用名
     */
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-DEPT-PV";

    @GetMapping("/op/dept/get/{id}")
    public Dept getDeptById(@PathVariable("id") Integer id) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
    }

    @PostMapping("/op/dept/add")
    public boolean addDept(Dept dept) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
    }

    @GetMapping("/op/dept/list")
    public List<Dept> deptList() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
    }

}
